package com.lebron.kotlin.ui.activity

import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.transition.Explode
import android.transition.Slide
import com.lebron.kotlin.R
import com.lebron.kotlin.bean.movie.Subject
import com.lebron.kotlin.ui.helper.GlideApp
import com.lebron.kotlin.ui.present.impl.MovieDetailPresentImpl
import com.lebron.kotlin.ui.view.MovieDetailView
import com.lebron.kotlin.utils.IntentUtils
import com.lebron.kotlin.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_movie_detail.*

class MovieDetailActivity : BaseActivity(), MovieDetailView {
    override fun setMovieSubject(subject: Subject) {
    }

    override fun onError(error: String) {
        ToastUtil.toast(error)
//        adapter?.loadMoreComplete()
    }

    private val present = MovieDetailPresentImpl()

    private var id: String? = ""

    override fun getLayoutId(): Int {
        return R.layout.activity_movie_detail
    }

    override fun initView() {
        present.attachView(this)


        immersionBar?.titleBar(toolbar)?.init()
        toolbar.setNavigationIcon(R.drawable.vector_drawable_back)
        toolbar.setOnClickListener { onBackPressed() }

        toolbar.inflateMenu(R.menu.toolbar_menu_share)

        toolbar.menu.getItem(0).setOnMenuItemClickListener { _ ->
            IntentUtils.share(this, "分享一下", null)
            true
        }
    }

    override fun initData() {
        id = intent.getStringExtra("id")
        val imgRes: Bitmap? = intent.getParcelableExtra("movie_img")

        if (imgRes != null)
            iv_movie_img.setImageBitmap(imgRes)
        else {
            GlideApp.with(this)
                    .asBitmap()
                    .load(intent.getStringExtra("imgUrl"))
                    .into(iv_movie_img)
        }
        toolbar.title = intent.getStringExtra("title")
        //获取电影详情信息
        present.getMovieDetail(id!!)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val explode = Explode()
            val slide = Slide()
            slide.duration = 1000
            explode.duration = 1000
//            window.enterTransition = explode
            window.exitTransition = slide
        }

        super.onCreate(savedInstanceState)

    }

    override fun onBackPressed() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition()
        } else {
            super.onBackPressed()
        }
    }

    override fun onDestroy() {
        super.onDestroy()

        present.detachView()
    }
}
